#!/usr/bin/env python
# encoding: utf-8
import pandas as pd
import time
from feature_set.loan.un.utils.loan_un_order_features import *


class LoanUnOrderV3(object):

    @staticmethod
    def get_features(country_id, rst_data):
        st_time = time.time()
        current_info = rst_data['current_info']
        user_history_order = rst_data['user_history_order']
        current_apply_time = current_info['current_apply_time']
        current_acq_channel = current_info['current_acq_channel']
        current_loan_amount = current_info['current_loan_amount']
        current_product_code = current_info['current_product_code']
        register_time = current_info['register_time']

        user_history_order = get_order_format(user_history_order,
                                              current_apply_time=current_apply_time,
                                              register_time=register_time,
                                              country_id=country_id)
        user_history_order = user_history_order.sort_values(by='app_order_id', ascending=False)
        all_order_features = []
        current_order_features = get_current_order_features(user_history_order=user_history_order,
                                                            current_apply_time=current_apply_time,
                                                            register_time=register_time,
                                                            current_acq_channel=current_acq_channel,
                                                            current_product_code=current_product_code,
                                                            country_id=country_id)
        all_order_features.append(current_order_features)

        for feature_type in ['all', 'self_app', 'other_app', 'self_product', 'other_product']:
            tmp_first_features = get_first_features(user_history_order,
                                                    feature_type=feature_type,
                                                    country_id=country_id,
                                                    current_loan_amount=current_loan_amount,
                                                    current_apply_time=current_apply_time)
            all_order_features.append(tmp_first_features)
            tmp_last_features = get_last_features(user_history_order,
                                                  feature_type=feature_type,
                                                  country_id=country_id,
                                                  current_loan_amount=current_loan_amount,
                                                  current_apply_time=current_apply_time)
            all_order_features.append(tmp_last_features)
            tmp_sta_features = get_order_sta_features(user_history_order,
                                                      day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                      feature_type=feature_type)
            all_order_features.append(tmp_sta_features)

            tmp_rd_features = get_order_rd_features(user_history_order,
                                                    records_list=[1, 3, 7, 15, 30],
                                                    feature_type=feature_type)
            all_order_features.append(tmp_rd_features)

            tmp_day_features = get_order_day_features(user_history_order,
                                                      day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                      current_apply_time=current_apply_time,
                                                      feature_type=feature_type)
            all_order_features.append(tmp_day_features)

            tmp_continue_features = get_order_continue_features(user_history_order,
                                                                day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                                                current_apply_time=current_apply_time,
                                                                feature_type=feature_type)
            all_order_features.append(tmp_continue_features)

        merged_dict = {}
        for d in all_order_features:
            merged_dict.update(d)
        merged_df = pd.DataFrame(merged_dict, index=[0])
        merged_df.fillna(-999, inplace=True)
        return merged_df.to_dict(orient='records')[0]
